package edu.hubu.talentmanagementsystem.common;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;

/**
 * @author moonlan
 * date 2021/2/7 下午3:20
 */
@SuppressWarnings(value = "unused")
@ApiModel("标准JSON返回对象")
public class CommonResult implements Serializable {

    @ApiModelProperty("状态码")
    private int code;
    @ApiModelProperty("返回信息")
    private String msg;
    @ApiModelProperty("返回数据")
    private Object data;
    @ApiModelProperty("返回某种数据的总页数")
    private int totalPage;
    @ApiModelProperty("当前页数")
    private int currentPage;

    private final static ObjectMapper mapper = new ObjectMapper();

    public int getCode() {
        return code;
    }

    public CommonResult setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public CommonResult setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Object getData() {
        return data;
    }

    public CommonResult setData(Object data) {
        this.data = data;
        return this;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public CommonResult setTotalPage(int totalPage) {
        this.totalPage = totalPage;
        return this;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public CommonResult setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
        return this;
    }

    public static CommonResult success(Object data, int currentPage, int totalPage) {
        return new CommonResult().setMsg(HttpStatus.OK.getReasonPhrase())
                .setCode(HttpStatus.OK.value())
                .setCurrentPage(currentPage)
                .setData(data)
                .setTotalPage(totalPage);
    }

    public static CommonResult success(Object data) {
        return success(data, 1, 1);
    }

    public static CommonResult notFound(String msg) {
        return new CommonResult().setCode(HttpStatus.NOT_FOUND.value())
                .setMsg(msg)
                .setTotalPage(1)
                .setCurrentPage(1)
                .setData(null);
    }

    public static CommonResult notFound() {
        return notFound(HttpStatus.NOT_FOUND.getReasonPhrase());
    }

    public static CommonResult forbidden(String msg) {
        return new CommonResult().setCode(HttpStatus.FORBIDDEN.value())
                .setMsg(msg)
                .setTotalPage(1)
                .setCurrentPage(1)
                .setData(null);
    }

    public static CommonResult forbidden() {
        return forbidden(HttpStatus.FORBIDDEN.getReasonPhrase());
    }

    public static CommonResult badRequest(String msg) {
        return new CommonResult().setCode(HttpStatus.BAD_REQUEST.value())
                .setMsg(msg)
                .setTotalPage(1)
                .setCurrentPage(1)
                .setData(null);
    }

    public static CommonResult badRequest() {
        return badRequest(HttpStatus.BAD_REQUEST.getReasonPhrase());
    }

    public static CommonResult internalServerError(String msg) {
        return new CommonResult().setCode(HttpStatus.INTERNAL_SERVER_ERROR.value())
                .setMsg(msg)
                .setTotalPage(1)
                .setCurrentPage(1)
                .setData(null);
    }

    public static CommonResult internalServerError() {
        return badRequest(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
    }

    public static void write(HttpServletRequest request, HttpServletResponse response, Exception e) throws IOException {
        PrintWriter writer = response.getWriter();
        response.setContentType("application/json;charset=utf8");
        CommonResult result =
                new CommonResult().setCode(response.getStatus()).setMsg(e.getMessage()).setData(null).setTotalPage(0).setCurrentPage(0);

        writer.write(mapper.writeValueAsString(result));
    }

}
